/* This file is part of SpaceSim.
 
 SpaceSim is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 SpaceSim is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with SpaceSim.  If not, see <http://www.gnu.org/licenses/>. */

#define GRAPHICS_CPP

#include <sstream>

#include "graphics.h"

#include "main.h"
#include "planet.h"
#include "player.h"
#include "star.h"
#include "system.h"
#include "time.h"

template <class T>
inline std::string toString (const T& t) {
	std::stringstream str;
	str << t;
	return str.str();
}

sf::RenderWindow Graphics::App(sf::VideoMode(800, 600), "SpaceSim");

sf::View Graphics::PlayerCenter(sf::FloatRect(0, 0, 800, 600));

std::stack<Graphics::ViewMode::Enum> Graphics::ViewStack;

sf::String Graphics::ErrorString("Everything is okay!");

float Graphics::ZoomFactor = 1.0;

void Graphics::Draw() {
	// Clear screen
	Graphics::App.Clear();

	switch (ViewStack.top()) {
		case Graphics::ViewMode::NoView:
		default:
			GraphicsPrivate::DrawNoView();
			break;
		case Graphics::ViewMode::SystemView:
			GraphicsPrivate::DrawSystemView();
			break;
		case Graphics::ViewMode::ErrorView:
			GraphicsPrivate::DrawErrorView();
			break;
		case Graphics::ViewMode::SystemInfoView:
			GraphicsPrivate::DrawSystemInfoView();
			break;
		case Graphics::ViewMode::WarpSelectView:
			GraphicsPrivate::DrawWarpSelectView();
			break;
		case Graphics::ViewMode::WarpHubView:
			GraphicsPrivate::DrawWarpHubView();
			break;
		case Graphics::ViewMode::StationView:
			GraphicsPrivate::DrawStationView();
			break;
		case Graphics::ViewMode::MissionView:
			GraphicsPrivate::DrawMissionView();
			break;
	}

	// Finally, display the rendered frame on screen
	Graphics::App.Display();
}

void GraphicsPrivate::DrawNoView() {
	// Draw nothing
}

void GraphicsPrivate::DrawSystemView() {
	Graphics::PlayerCenter.SetCenter(player.GetX() * Graphics::ZoomFactor, player.GetY() * Graphics::ZoomFactor);
	Graphics::App.SetView(Graphics::PlayerCenter);
	// Placeholder: When there's a way for the player to travel between systems, clean this up
	// draw a system
	// Draw the stars on the background
	for (int i = 0; i < NUM_STARS; i++) {
		Stars[i].Draw();
	}
	
	Main::CurrentCluster.GetSystem(Main::SubSystem)->Draw();
	
	// draw the player
	Graphics::App.Draw(player.GetSprite());	// draw!
	sf::String typeString(toString(player.GetOrbitFraction()));
	typeString.SetSize(15);
	typeString.SetPosition(player.GetX() * Graphics::ZoomFactor, player.GetY() * Graphics::ZoomFactor);
	Graphics::App.Draw(typeString);
	
	Main::Message.SetPosition(player.GetX() * Graphics::ZoomFactor - 380, player.GetY() * Graphics::ZoomFactor - 280);
	Graphics::App.Draw(Main::Message);
}

void GraphicsPrivate::DrawErrorView() {
	Graphics::App.SetView(Graphics::App.GetDefaultView());

	Graphics::ErrorString.SetPosition(400, 300);
	Graphics::App.Draw(sf::Shape::Rectangle(0, 0, 800, 600, sf::Color::Red));
	Graphics::App.Draw(Graphics::ErrorString);
}

void GraphicsPrivate::DrawSystemInfoView() {
	Graphics::App.SetView(Graphics::App.GetDefaultView());

	Main::CurrentCluster.GetSystem(Main::SubSystem)->DrawInfo();
}

void GraphicsPrivate::DrawWarpSelectView() {
	Graphics::App.SetView(Graphics::App.GetDefaultView());

	Main::CurrentCluster.DrawSubWarp();
}

void GraphicsPrivate::DrawWarpHubView() {
	Graphics::App.SetView(Graphics::App.GetDefaultView());

	Main::CurrentCluster.DrawHubWarp();
}

void GraphicsPrivate::DrawStationView() {
	Graphics::App.SetView(Graphics::App.GetDefaultView());

	player.GetPlanet()->DrawStation();
	Main::Message.SetPosition(20, 20);
	Graphics::App.Draw(Main::Message);
}

void GraphicsPrivate::DrawMissionView() {
	Graphics::App.SetView(Graphics::App.GetDefaultView());
	
	sf::String MissionString(player.GetMission()->GetDesc());
	MissionString.SetSize(15);
	MissionString.SetPosition(20, 20);
	Graphics::App.Draw(MissionString);
}